package com.zabra.zl.ui.recharge;

import android.Manifest;
import android.app.AlertDialog;
import android.app.Dialog;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.zabra.zl.Config;
import com.zabra.zl.R;
import com.zabra.zl.base.BaseMvpActivity;
import com.zabra.zl.data.RechargeOffLineBean;
import com.zabra.zl.data.RechargeOnLineBean;
import com.zabra.zl.data.SimpleBean;
import com.zabra.zl.data.WechatBean;
import com.zabra.zl.ui.recharge.contract.RechargeContract;
import com.zabra.zl.ui.recharge.presenter.RechargePresenter;
import com.zabra.zl.ui.success.RechargeSuccessActivity;
import com.zabra.zl.utils.BaseUtils;
import com.zabra.zl.utils.DialogUtils;
import com.zabra.zl.utils.MaptoJsonUtils;
import com.zabra.zl.utils.ShowDialogUtils;
import com.zabra.zl.utils.ToastUtil;
import com.zhy.m.permission.MPermissions;
import com.zhy.m.permission.PermissionDenied;
import com.zhy.m.permission.PermissionGrant;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 充值
 */
public class RechargeActivity extends BaseMvpActivity<RechargePresenter> implements RechargeContract.View {


    @BindView(R.id.back)
    LinearLayout back;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.tv_money)
    TextView tvMoney;
    @BindView(R.id.cb_zz)
    CheckBox cbZz;
    @BindView(R.id.rl_zz)
    RelativeLayout rlZz;
    @BindView(R.id.cb_wechat)
    CheckBox cbWechat;
    @BindView(R.id.rl_wechat)
    RelativeLayout rlWechat;
    @BindView(R.id.cb_alipay)
    CheckBox cbAlipay;
    @BindView(R.id.rl_alipay)
    RelativeLayout rlAlipay;
    @BindView(R.id.bt_next)
    TextView btNext;
    //默认选中转账
    private String rechargeStyle = Config.EFT;
    private String strMoney, strAccout, strBlance, strReceiveCardId, strImgUrl;
    private TextView tv_user, tv_bank, tv_address, tv_code;
    private Dialog dialog;
    private AlertDialog dialogoffline;
    private ImageView iv_zxing;

    @Override
    public int getLayoutId() {
        return R.layout.activity_recharge;
    }

    @Override
    public void initView() {
        mPresenter = new RechargePresenter();
        mPresenter.attachView(this);
        strBlance = getIntent().getExtras().getString("blance");
        cbZz.setEnabled(false);
        cbWechat.setEnabled(false);
        cbAlipay.setEnabled(false);
        tvTitle.setText("充值");
        tvMoney.setText(strBlance + "元");
    }

    @Override
    public void showLoading() {
        dialog = DialogUtils.showDialog_progressbar(this);
    }

    @Override
    public void hideLoading() {
        DialogUtils.closeDialog(dialog);
    }

    @Override
    public void onError(Throwable throwable) {
        DialogUtils.closeDialog(dialog);
    }

    @Override
    public void onSuccess(RechargeOnLineBean bean) {
        if (bean.getRespCode().equals(Config.SUCCESS)) {
            tv_user.setText(bean.getFyAccNm());
            tv_bank.setText(bean.getFyAccNo());
            tv_address.setText(bean.getFyBankBranch());
            tv_code.setText(bean.getChgCd());
        } else if (bean.getRespCode().equals(Config.TOKENOUT)) {
            ShowDialogUtils.restLoginDialog(this);
        } else {
            ToastUtil.showToast(this, bean.getRespDesc());
        }
    }

    @Override
    public void onSuccess(RechargeOffLineBean bean) {
        if (bean.getRespCode().equals(Config.SUCCESS)) {
            tv_user.setText(bean.getCard().getCardName());
            tv_bank.setText(bean.getCard().getCardNo());
            tv_address.setText(bean.getCard().getBankBranchName());
            strReceiveCardId = bean.getCard().getCardId() + "";
        } else if (bean.getRespCode().equals(Config.TOKENOUT)) {
            ShowDialogUtils.restLoginDialog(this);
        } else {
            ToastUtil.showToast(this, bean.getRespDesc());
        }
    }

    @Override
    public void onSuccess(SimpleBean bean) {
        if (bean.getRespCode().equals(Config.SUCCESS)) {
            dialogoffline.dismiss();
            toClass1(this, RechargeSuccessActivity.class);
        } else if (bean.getRespCode().equals(Config.TOKENOUT)) {
            ShowDialogUtils.restLoginDialog(this);
        } else {
            ToastUtil.showToast(this, bean.getRespDesc());
        }
    }

    @Override
    public void onSuccess(WechatBean bean) {
        if (bean.getRespCode().equals(Config.SUCCESS)) {
            strImgUrl = bean.getPath();
            Glide.with(this).load(strImgUrl).into(iv_zxing);
            iv_zxing.setOnLongClickListener(new View.OnLongClickListener() {
                @Override
                public boolean onLongClick(View view) {
                    MPermissions.requestPermissions(RechargeActivity.this, 1, Manifest.permission.WRITE_EXTERNAL_STORAGE);
                    return false;
                }
            });
        } else if (bean.getRespCode().equals(Config.TOKENOUT)) {
            ShowDialogUtils.restLoginDialog(this);
        } else {
            ToastUtil.showToast(this, bean.getRespDesc());
        }
    }

    @PermissionGrant(1)
    public void onPermissionGrant() {
        ToastUtil.showToast(RechargeActivity.this, "正在保存图片");
        BaseUtils.returnBitMap(RechargeActivity.this, strImgUrl);
    }

    @PermissionDenied(1)
    public void onPermissionDenied() {
        ToastUtil.showToast(RechargeActivity.this, "请打开写入文件权限");
    }

    @OnClick({R.id.back, R.id.rl_zz, R.id.rl_wechat, R.id.rl_alipay, R.id.bt_next})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.back:
                finish();
                break;
            case R.id.rl_zz:
                cbZz.setChecked(true);
                cbWechat.setChecked(false);
                cbAlipay.setChecked(false);
                rechargeStyle = Config.EFT;
                break;
            case R.id.rl_wechat:
                cbZz.setChecked(false);
                cbWechat.setChecked(true);
                cbAlipay.setChecked(false);
                rechargeStyle = Config.WECHAT;
                break;
            case R.id.rl_alipay:
                cbZz.setChecked(false);
                cbWechat.setChecked(false);
                cbAlipay.setChecked(true);
                rechargeStyle = Config.ALIPAY;
                break;
            case R.id.bt_next:
                switch (rechargeStyle) {
                    case Config.EFT:
                        //线上转账
//                        showInput();
                        //线下转账
                        showDialogOffline();
                        break;
                    case Config.WECHAT:
                        showDialogWeChat();
                        break;
                    case Config.ALIPAY:
                        showDialogAlipay();
                        break;
                }
                break;
        }
    }

    private void showInput() {
        final AlertDialog dialog = new AlertDialog.Builder(this).create();
        LayoutInflater layoutInflater = this.getLayoutInflater();
        View view = layoutInflater.inflate(R.layout.dialog_rechargeinput, null);
        dialog.setView(view);
        dialog.setCanceledOnTouchOutside(false);
        dialog.show();
        WindowManager.LayoutParams params = dialog.getWindow().getAttributes();
        params.width = ViewGroup.LayoutParams.MATCH_PARENT;
        params.height = ViewGroup.LayoutParams.WRAP_CONTENT;
        Window window = dialog.getWindow();
        window.setAttributes(params);
        window.setBackgroundDrawableResource(android.R.color.transparent);
        EditText et_money = view.findViewById(R.id.dialog_money);
        EditText et_account = view.findViewById(R.id.dialog_account);
        view.findViewById(R.id.iv_exit).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                dialog.dismiss();
            }
        });
        view.findViewById(R.id.dialog_next).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                strMoney = et_money.getText().toString().trim();
                strAccout = et_account.getText().toString().trim();
                if (TextUtils.isEmpty(strMoney) || TextUtils.isEmpty(strAccout)) {
                    ToastUtil.showToast(view.getContext(), "请输入金额和账号");
                } else {
                    dialog.dismiss();
                    showDialogOnline();
                }

            }
        });


    }

    /**
     * 线上对公转账
     */
    private void showDialogOnline() {
        final AlertDialog dialog = new AlertDialog.Builder(this).create();
        LayoutInflater layoutInflater = this.getLayoutInflater();
        View view = layoutInflater.inflate(R.layout.dialog_rechargeonline, null);
        dialog.setView(view);
        dialog.setCanceledOnTouchOutside(false);
        dialog.show();
        WindowManager.LayoutParams params = dialog.getWindow().getAttributes();
        params.width = ViewGroup.LayoutParams.MATCH_PARENT;
        params.height = ViewGroup.LayoutParams.WRAP_CONTENT;
        Window window = dialog.getWindow();
        window.setAttributes(params);
        window.setBackgroundDrawableResource(android.R.color.transparent);
        keyList.add("debtorId");
        keyList.add("amt");
        keyList.add("cardNo");
        valueList.add(strUserid);
        valueList.add(strMoney);
        valueList.add(strAccout);
        mPresenter.getOnLineData(MaptoJsonUtils.Simple(keyList, valueList));
        keyList.clear();
        valueList.clear();

        tv_user = view.findViewById(R.id.dialog_user);
        tv_bank = view.findViewById(R.id.dialog_bank);
        tv_address = view.findViewById(R.id.dialog_bank_address);
        tv_code = view.findViewById(R.id.dialog_code);
        view.findViewById(R.id.tv_copy_user).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                BaseUtils.copyMothod(view.getContext(), tv_user.getText().toString());

            }
        });
        view.findViewById(R.id.tv_copy_bank).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                BaseUtils.copyMothod(view.getContext(), tv_bank.getText().toString().trim());
            }
        });
        view.findViewById(R.id.tv_copy_address).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                BaseUtils.copyMothod(view.getContext(), tv_address.getText().toString());
            }
        });
        view.findViewById(R.id.tv_copy_code).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                BaseUtils.copyMothod(view.getContext(), tv_code.getText().toString());
            }
        });
        view.findViewById(R.id.iv_exit).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                dialog.dismiss();
            }
        });
    }

    /**
     * 线下对公转账
     */
    private void showDialogOffline() {
        dialogoffline = new AlertDialog.Builder(this).create();
        LayoutInflater layoutInflater = this.getLayoutInflater();
        //线下
        View view = layoutInflater.inflate(R.layout.dialog_rechargeoffline, null);
        dialogoffline.setView(view);
        dialogoffline.setCanceledOnTouchOutside(false);
        dialogoffline.show();
        WindowManager.LayoutParams params = dialogoffline.getWindow().getAttributes();
        params.width = ViewGroup.LayoutParams.MATCH_PARENT;
        params.height = ViewGroup.LayoutParams.WRAP_CONTENT;
        Window window = dialogoffline.getWindow();
        window.setAttributes(params);
        window.setBackgroundDrawableResource(android.R.color.transparent);
        keyList.add("debtorId");
        valueList.add(strUserid);
        mPresenter.getOffLineData(MaptoJsonUtils.Simple(keyList, valueList));

        tv_user = view.findViewById(R.id.dialog_user);
        tv_bank = view.findViewById(R.id.dialog_bank);
        tv_address = view.findViewById(R.id.dialog_address);
        EditText et_money = view.findViewById(R.id.et_money);
        EditText et_account = view.findViewById(R.id.et_account);
        view.findViewById(R.id.tv_copy_user).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                BaseUtils.copyMothod(view.getContext(), tv_user.getText().toString());

            }
        });
        view.findViewById(R.id.tv_copy_bank).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                BaseUtils.copyMothod(view.getContext(), tv_bank.getText().toString().trim());
            }
        });
        view.findViewById(R.id.tv_copy_address).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                BaseUtils.copyMothod(view.getContext(), tv_address.getText().toString());
            }
        });
        view.findViewById(R.id.iv_exit).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                dialogoffline.dismiss();
            }
        });
        view.findViewById(R.id.tv_submit).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                strMoney = et_money.getText().toString().trim();
                strAccout = et_account.getText().toString().trim();
                if (TextUtils.isEmpty(strMoney) || TextUtils.isEmpty(strAccout)) {
                    ToastUtil.showToast(view.getContext(), "请输入金额和账号");
                } else {
                    keyList.add("debtorId");
                    keyList.add("amt");
                    keyList.add("receiveCardId");
                    keyList.add("cardNo");
                    valueList.add(strUserid);
                    valueList.add(strMoney);
                    valueList.add(strReceiveCardId);
                    valueList.add(strAccout);
                    mPresenter.getOffLineSubmitData(MaptoJsonUtils.Simple(keyList, valueList));
                    keyList.clear();
                    valueList.clear();
                }

            }
        });
    }

    /**
     * 微信充值
     */
    private void showDialogWeChat() {
        dialogoffline = new AlertDialog.Builder(this).create();
        LayoutInflater layoutInflater = this.getLayoutInflater();
        View view = layoutInflater.inflate(R.layout.dialog_wechat, null);
        dialogoffline.setView(view);
        dialogoffline.setCanceledOnTouchOutside(false);
        dialogoffline.show();
        WindowManager.LayoutParams params = dialogoffline.getWindow().getAttributes();
        params.width = ViewGroup.LayoutParams.MATCH_PARENT;
        params.height = ViewGroup.LayoutParams.WRAP_CONTENT;
        Window window = dialogoffline.getWindow();
        window.setAttributes(params);
        window.setBackgroundDrawableResource(android.R.color.transparent);
        EditText et_money = view.findViewById(R.id.dialog_money);
        EditText et_account = view.findViewById(R.id.dialog_account);
        iv_zxing = view.findViewById(R.id.img_zxing);
        keyList.add("debtorId");
        valueList.add(strUserid);
        mPresenter.getWeChatData(MaptoJsonUtils.Simple(keyList, valueList));
        view.findViewById(R.id.iv_exit).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                dialogoffline.dismiss();
            }
        });

        view.findViewById(R.id.tv_submit).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                strMoney = et_money.getText().toString().trim();
                strAccout = et_account.getText().toString().trim();
                if (TextUtils.isEmpty(strMoney) || TextUtils.isEmpty(strAccout)) {
                    ToastUtil.showToast(view.getContext(), "请输入金额和账号");
                } else {
                    keyList.add("debtorId");
                    keyList.add("phone");
                    keyList.add("amt");
                    valueList.add(strUserid);
                    valueList.add(strAccout);
                    valueList.add(strMoney);

                    mPresenter.getSubmitWeChatData(MaptoJsonUtils.Simple(keyList, valueList));
                    keyList.clear();
                    valueList.clear();
                }

            }
        });
    }

    /**
     * 支付宝充值
     */
    private void showDialogAlipay() {
        dialogoffline = new AlertDialog.Builder(this).create();
        LayoutInflater layoutInflater = this.getLayoutInflater();
        View view = layoutInflater.inflate(R.layout.dialog_alipay, null);
        dialogoffline.setView(view);
        dialogoffline.setCanceledOnTouchOutside(false);
        dialogoffline.show();
        WindowManager.LayoutParams params = dialogoffline.getWindow().getAttributes();
        params.width = ViewGroup.LayoutParams.MATCH_PARENT;
        params.height = ViewGroup.LayoutParams.WRAP_CONTENT;
        Window window = dialogoffline.getWindow();
        window.setAttributes(params);
        window.setBackgroundDrawableResource(android.R.color.transparent);
        EditText et_money = view.findViewById(R.id.dialog_money);
        EditText et_account = view.findViewById(R.id.dialog_account);
        iv_zxing = view.findViewById(R.id.img_zxing);
        view.findViewById(R.id.iv_exit).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                dialogoffline.dismiss();
            }
        });
        keyList.add("debtorId");
        valueList.add(strUserid);
        mPresenter.getAlipayData(MaptoJsonUtils.Simple(keyList, valueList));

        view.findViewById(R.id.tv_submit).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                strMoney = et_money.getText().toString().trim();
                strAccout = et_account.getText().toString().trim();
                if (TextUtils.isEmpty(strMoney) || TextUtils.isEmpty(strAccout)) {
                    ToastUtil.showToast(view.getContext(), "请输入金额和账号");
                } else {
                    keyList.add("debtorId");
                    keyList.add("phone");
                    keyList.add("amt");
                    valueList.add(strUserid);
                    valueList.add(strAccout);
                    valueList.add(strMoney);

                    mPresenter.getSubmitAlipayData(MaptoJsonUtils.Simple(keyList, valueList));
                    keyList.clear();
                    valueList.clear();
                }

            }
        });
    }
}
